<?php
	if(!isset($_SESSION)){
		session_start();
	}
	
	// Clear previous messages.
	if(isset($_SESSION['error'])) session_unregister('error');
	if(isset($_SESSION['success'])) session_unregister('success');

	// Get values from the form.
	$itemCode = $_POST['itemCode'];
	$batchId = $_POST['batchId'];
	$orderQuantity = $_POST['orderQuantity'];
	
	
	
	// Check for validation.
	if($itemCode == "" || $batchId=="" || $orderQuantity==0){
		$_SESSION['error'] = "Pleace complete the mandatory fields";		
	}else{
		include("../dbconnection.php");
		
		// Get price form db
		if($result = mysql_query("SELECT unitPrice FROM stock WHERE itemCode='$itemCode' AND batchId='$batchId'")){
			$price = mysql_fetch_assoc($result);
		
			
			if(mysql_query("INSERT INTO order_items (itemCode, batchid, quantity, price) VALUES ('$itemCode','$batchId','$orderQuantity','$price)")){
				$_SESSION['success'] = "Item added to order successfully.";
			}else{
				$_SESSION['error'] = "Error occured!";
			}
		}else{
			$_SESSION['error'] = "Error occured!";
		}	
		
	}
	$_SESSION['page'] = "addOrders.php";
	header('Location: ../../');
?>s